import * as actionTypes from './constants';
// 导入 请求的接口
import { getSongDetail, getSongLyric } from '../../../api/gequApi'
// 导入 自己封装的方法
import { parseLyric } from '../../../utils/parse-lyric'
import { getRandomNumber } from '../../../utils/math-utils'

// 改变当前 播放 歌曲的信息
const changeCurrentSongAction = (currentSong) => ({
  type: actionTypes.CHANGE_CURRENT_SONG,
  currentSong
})
//  改变 播放列表的信息
const changePlayListAction = (playList) => ({
  type: actionTypes.CHANGE_PLAY_LIST,
  playList
});
//  当前播放歌曲，在 播放列表 的索引值
const changeCurrentSongIndexAction = (index) => ({
  type: actionTypes.CHANGE_CURRENT_SONG_INDEX,
  index
});
//  获取歌词
const changLyricListAction = (lyricList) => ({
  type: actionTypes.CHANGE_LYRIC_LIST,
  lyricList
})


// 对外暴露的  action
// 获取歌词
export const getSongLyricAction = (id) => {
  return dispatch => {
    getSongLyric(id).then(res => {
      if (res.code === 200) {
        const lyric = res.lrc.lyric
        const lyricList = parseLyric(lyric)
        dispatch(changLyricListAction(lyricList))
      }
    })
  }
}

// 对外暴露， 获取歌曲详情的接口
export const getSongDetailAction = (ids => {
  return (dispatch, getState) => {
    // 根据  id 查找 playlist 中 是否已经有了该歌曲
    const playList = getState().getIn(["player", "playList"])
    const songIndex = playList.findIndex(song => song.id === ids)
    // 判断是否找到了歌曲
    let song = null
    if (songIndex !== -1) {
      // 找到了此歌曲
      dispatch(changeCurrentSongIndexAction(songIndex))
      song = playList[songIndex] //  歌曲信息
      // song.playing = true  // 添加是否正在播放的 标志
      dispatch(changeCurrentSongAction(song))
      // 获取歌词
      dispatch(getSongLyricAction(song.id))
    } else {
      // 没有找到这个歌曲
      getSongDetail(ids).then(res => {
        if (res.code === 200) {
          song = res.songs && res.songs[0];
          if (!song) return;

          // song.playing = true  // 添加是否正在播放的 标志
          // 添加到播放列表中
          const newPlayList = [...playList];
          newPlayList.push(song);

          // 更新redux中的值
          dispatch(changePlayListAction(newPlayList));
          dispatch(changeCurrentSongIndexAction(newPlayList.length - 1));
          dispatch(changeCurrentSongAction(song));

          // 请求歌词
          dispatch(getSongLyricAction(song.id))
        }
      })

    }
  }

})

//  改变播放状态 随机  0 列表循环 1 随机 2 单曲
export const changeSequenceAction = (sequence) => ({
  type: actionTypes.CHANGE_SEQUENCE,
  sequence
});

export const changeCurrentLyricIndexAction = (index) => ({
  type: actionTypes.CHANGE_CURRENT_LYRIC_INDEX,
  index
})

export const changeCurrentIndexAndSongAction = (tag) => {
  return (dispatch, getState) => {
    const playList = getState().getIn(["player", "playList"]);
    const sequence = getState().getIn(["player", "sequence"]);
    let currentSongIndex = getState().getIn(["player", "currentSongIndex"]);

    switch (sequence) {
      case 1: // 随机播放
        let randomIndex = getRandomNumber(playList.length);
        while (randomIndex === currentSongIndex) {
          randomIndex = getRandomNumber(playList.length);
        }
        currentSongIndex = randomIndex;
        break;
      default: // 顺序播放
        currentSongIndex += tag;
        if (currentSongIndex >= playList.length) currentSongIndex = 0;
        if (currentSongIndex < 0) currentSongIndex = playList.length - 1;
    }

    const currentSong = playList[currentSongIndex];
    dispatch(changeCurrentSongAction(currentSong));
    dispatch(changeCurrentSongIndexAction(currentSongIndex));

    // 请求歌词
    dispatch(getSongLyricAction(currentSong.id));
  }
}

// 清空列表歌曲
export const deleteBofangListAction = () => ({
  type: actionTypes.DELETE_LIEST,
})


export const deteleOneAction = (id => {
  return (dispatch, getState) => {
    const playList = getState().getIn(["player", "playList"])
    const currentSongIndex = getState().getIn(["player", "currentSongIndex"])

    if (playList[currentSongIndex].id == id) {
      console.log('正在播放');
      // 要删除正在播放的这个歌曲
      const newPlayList = playList.filter(item => {
        if (item.id != id) {
          return item
        }
      })
      dispatch(changePlayListAction(newPlayList))
      dispatch(changeCurrentSongAction(newPlayList[0]))
      dispatch(changeCurrentSongIndexAction(0))
      dispatch(getSongLyricAction(newPlayList[0].id))
    }
  }
})